Total Complexity | 6 |
Total Lines | 46 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | import { Token, TokenType } from "./Token" |
||
6 | |||
7 | /** |
||
8 | * @class Parser |
||
9 | * @name Parser |
||
10 | */ |
||
11 | 2 | export class Parser { |
|
12 | tokens: Generator<Token> |
||
13 | 7 | identifiers: Identifiers<string> = {} |
|
14 | |||
15 | /** |
||
16 | * Initialize a parser from a token generator. |
||
17 | */ |
||
18 | constructor(tokens: Generator<Token>) { |
||
19 | 7 | this.tokens = tokens |
|
20 | } |
||
21 | |||
22 | /** |
||
23 | * Evaluate the result. |
||
24 | */ |
||
25 | evaluate(): bigint { |
||
26 | 6 | let n = 0n |
|
27 | 6 | for(const x of this.tokens) { |
|
28 | 8 | if (x.type === TokenType.identifier) { |
|
29 | 3 | if (!(x.s in this.identifiers)) { |
|
30 | 2 | throw `"${x.s}" is undefined` |
|
31 | } |
||
32 | 1 | n += BigInt(this.identifiers[x.s]) |
|
33 | } |
||
34 | // else if (x.type === TokenType.separator) { |
||
35 | // } |
||
36 | // else if (x.type === TokenType.operator) { |
||
37 | // if (x.s === '=') { |
||
38 | // } |
||
39 | // } |
||
40 | 5 | else if (x.type === TokenType.literal) { |
|
41 | 3 | n += BigInt(x.s) |
|
42 | } |
||
43 | } |
||
44 | 4 | return n |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * The text representation. |
||
49 | */ |
||
50 | toString(): string { |
||
51 | 2 | return this.evaluate().toString() |
|
52 | } |
||
57 |